home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / mmailp.idb / usr / lib / Zmail / samples / zscript / chrono.zsc.z / chrono.zsc
Encoding:
Text File  |  1997-01-22  |  2.9 KB  |  99 lines

  1. # Sample Z-Script  function: chrono
  2. #              button: Chrono
  3.  
  4. function chrono() {
  5. #
  6. # Chronological sort, grouping by identical subject (message thread).
  7. #
  8. #%
  9. # This function groups messages by subject, sorts by date within the
  10. # subject, and sorts the subject groups by the first date in each
  11. # group.  This permits easy reading of related messages, in order.
  12. #
  13. # If chrono quits with a "Call stack too deep" error, you may run
  14. # it again -- it will resume where it left off and finish the sort.
  15. #%
  16.     # This is a recursive function!  Make sure $recursive is set.
  17.     if ! $?recursive
  18.     set recursive = $0  # set recursive as a hint for later.
  19.     endif
  20.     # If we are first entering, initialize the beginning and end
  21.     # of the range to sort.  If an argument was given, use that
  22.     # as the place to begin the sort.
  23.     # Also create a few useful command shorthands for later.
  24.     #
  25.     if ! $?begin
  26.     unmark *
  27.     if $?1
  28.         set begin = "$1"
  29.     else
  30.         set begin = 1
  31.     endif
  32.     msg_list $ | set end
  33.     cmd next_number 'msg_list +'
  34.     cmd first_number 'msg_list - ; msg_list .'
  35.     cmd last_number 'msg_list + ; msg_list .'
  36.     endif
  37.     #
  38.     # Now sort all the messages from the beginning point
  39.     # through the end, by date ....
  40.     #
  41.     $begin - $ | sort -d | msg_list -
  42.     #
  43.     # Find the messages with the same subject as the current message.
  44.     # If the current message has no subject, find all messages that
  45.     # reference the message ID of the current message.  We could do
  46.     # both, but the Subject: header is cached whereas the References:
  47.     # header is not, so a search for references is slower.
  48.     #
  49.     # Mark any messages that match, including the current message.
  50.     #
  51.     if $?[%s]
  52.     eval -h pick -r $begin - $ -s %s$ | mark
  53.     else
  54.     eval -h pick -r $begin - $ -h references %i | mark .
  55.     endif
  56.     #
  57.     # Sort by priority to move the marked messages to the top.
  58.     #
  59.     $begin - $ | sort -p
  60.     #
  61.     # Find the range boundaries of the set of marked messages,
  62.     # then unmark them.
  63.     #
  64.     :m | first_number | set first
  65.     :m | last_number | set last
  66.     unmark *
  67.     #
  68.     # If there is a range of more than one message,
  69.     # re-sort that range by date, and unset the vars.
  70.     #
  71.     if $first != $last
  72.     $first - $last | sort -d | next_number
  73.     endif
  74.     unset first last
  75.     #
  76.     # Advance the beginning point to the next message beyond those sorted.
  77.     #
  78.     next_number | set begin
  79.     #
  80.     # If we've reached the end of the list, clean up and return
  81.     #
  82.     if $begin == $end
  83.     unset begin end
  84.     if $recursive == $0
  85.         unset recursive
  86.     endif
  87.     uncmd next_number first_number last_number
  88.     return 0
  89.     endif
  90.     #
  91.     # If we have more messages to sort, repeat.  This will abort
  92.     # without completing if there are more than 100 distinct subjects,
  93.     # because stack depth is limited.  The user can repeat the "chrono"
  94.     # command to continue sorting, because "begin" will still be set.
  95.     #
  96.     chrono
  97. }
  98. button -n Chrono chrono
  99.